<div class="layuimini-main">

    <div class="layui-form layuimini-form">
        <input type="hidden" name="id" value="{{ $data['info']['id'] }}">
        <div class="layui-form-item">
            <label class="layui-form-label">绑定菜单</label>
            <div class="layui-input-block">
                <label>
                    <select name="menu_id" lay-filter="pid">
                        <option value='0'>顶级菜单</option>
                        @foreach($data['parent'] as $v)
                            <option value="{{ $v['id'] }}">{{ $v['title'] }}</option>
                            @if(!empty($v['child']))
                                @foreach($v['child'] as $vv )
                                    <option value="{{ $vv['id'] }}"><span>|----</span>{{ $vv['title'] }}</option>
                                @endforeach
                            @endif
                        @endforeach
                    </select>
                </label>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" lay-reqtext="名称不能为空" placeholder="请输入名称" value="{{ $data['info']['name'] }}"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">数据库表名</label>
            <div class="layui-input-block">
                <input type="text" name="table_name" lay-verify="required|table_name" lay-reqtext="表名不能为空"
                       placeholder="请输入表名" value="{{ $data['info']['table_name'] }}" class="layui-input" disabled>
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label required">描述</label>
            <div class="layui-input-block">
                <textarea name="description" lay-verify="required" lay-reqtext="描述不能为空" class="layui-textarea"
                          placeholder="请输入描述">{{ $data['info']['description'] }}</textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form', 'iconPickerFa'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;
        var miniPage = layui.miniPage;

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.ajax({
                url: 'model/'+ data.field.id,
                method: 'PUT',
                data: data.field,
                success: function (res) {
                    console.log(res)
                    if (res.code === 0) {
                        layer.msg('保存成功', {icon: 1}, function () {
                            // 关闭弹框
                            layer.close(parentIndex);
                            // 刷新列表
                            var options = {};
                            options.homeInfo = options.homeInfo || {};
                            miniPage.refresh(options);
                        })
                    } else {
                        layer.msg(res.data, {icon: 5})
                    }
                },
                error: function (res) {
                    layer.msg('请求错误', {icon: 5})
                }
            });
            return false;
        });
    });
</script>
